import java.util.HashMap;
import java.util.Map;

/*
 * @lc app=leetcode.cn id=1 lang=java
 *
 * [1] 两数之和
 */

// @lc code=start
class Solution {
    public int[] twoSum(int[] nums, int target) {

        //solution 1
        /*
        int[] a = new int[2];
        for (int i =0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if(nums[i] + nums[j] == target) {
                    a[0] = i;
                    a[1] = j;
                    return a;
                }
            }
        }
        return null;
        */
        //solution 2
        Map<Integer, Integer> m = new HashMap<>();
        for(int i = 0; i < nums.length; i++) {
            if (m.get(target - nums[i]) != null) {
                return new int[] {m.get(target - nums[i]), i};
            }
            m.put(nums[i], i);
        }
        return new int[] {0, 0};
    }
}
// @lc code=end

